package com.es.esapi.controller.elasticsearch;

import com.es.esapi.common.CommonResponse;
import com.es.esapi.domain.ClassA;
import com.es.esapi.service.EsearchService;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;

@RestController
@Validated
@Valid
@RequestMapping("/esearch/")
public class EsearchController {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @Autowired
    private EsearchService esearchService;


    @PostMapping("create_index")
    public  CommonResponse<Object> CreateIndex(
            @RequestParam @NotBlank(message = "索引名不能为空") String index_name
    ) throws IOException {
        return  esearchService.create_index(index_name);
    }

    @PostMapping("index_ini")
    public  CommonResponse<Object> IndexIni(
    ) throws IOException {
        return  esearchService.index_ini();
    }

    @PostMapping("exist_index")
    public CommonResponse<Object> ExistIndex(
            @RequestParam @NotBlank(message = "索引名不能为空") String index_name
    ) throws IOException {
        return  esearchService.exist_index(index_name);
    }

    @PostMapping("delete_index")
    public CommonResponse<Object> DeleteIndex(
            @RequestParam @NotBlank(message = "索引名不能为空") String index_name
    ) throws IOException {
        return  esearchService.delete_index(index_name);
    }

    @PostMapping("add_document")
    public CommonResponse<Object> AddDocument(
            @RequestParam @NotBlank(message = "文档标题不能为空") String doc_title,
            @RequestParam @NotNull(message = "页面索引不能为空") int page_index,
            @RequestParam @NotBlank(message = "页面内容不能为空") String doc_content,
            String index_name
            ) throws IOException {
        return  esearchService.add_document(doc_title,page_index,doc_content,index_name);
    }

    @PostMapping("add_document_bulk")
    public CommonResponse<Object> AddDocumentBulk(
            @RequestParam @NotBlank(message = "文档标题不能为空") String doc_title
    ) throws IOException {
        ArrayList<ClassA> tlist = null;
        return  esearchService.bulk_document(doc_title,tlist);
    }
    @RequestMapping("is_exist_document")
    public CommonResponse<Object> IsExistDocument(
            @RequestParam @NotBlank(message = "文档标题不能为空") String doc_title,
            @RequestParam @NotNull(message = "页面索引不能为空") int page_index
    ) throws IOException {
        return  esearchService.is_exist_document(doc_title,page_index);
    }

    @PostMapping("delete_document")
    public CommonResponse<Object> DeleteDocument(
            @RequestParam @NotBlank(message = "文档标题不能为空") String doc_title,
            @RequestParam @NotNull(message = "页面索引不能为空") int page_index
    ) throws IOException {
        return  esearchService.delete_document(doc_title,page_index);
    }

    @RequestMapping("search")
    public CommonResponse<Object> Search(
            @RequestParam @NotBlank(message = "搜索关键词不能为空") String search_key,
            @RequestParam @NotNull(message = "搜索模式不能为空") int mode
    ) throws IOException {
        String search_mode="";
//        1 是精确查询
        if(mode==1){
            search_mode = "term";
        }
//        2 是模糊查询
        else if (mode==2){
            search_mode = "match";
        }
        return  esearchService.search(search_key,search_mode);
    }

    @Valid
    @PostMapping("favour")
    public CommonResponse<Object> Favour(
            @RequestParam @NotBlank(message = "文章标题不能为空") String title
    ) throws IOException {
        return  esearchService.favour(title+"_");
    }

    @PostMapping("click")
    public CommonResponse<Object> Click(
            @RequestParam @NotBlank(message = "文章标题不能为空") String title
    ) throws IOException {
        return  esearchService.click(title+"_");
    }






}
